home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Multiprocessing 2.1v2 SDK / Sample Code / MP Sort Picts 12⁄04⁄99 / sources / MPSortPicts.cp < prev    next >
Encoding:
Text File  |  1999-11-30  |  8.6 KB  |  416 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    MPSortPicts a demo of MP Tasking
  5. **
  6. **    by Matthew Xavier Mora, Former Apple Developer Technical Support
  7. **
  8. **    File:        SortPicts.cp
  9. **
  10. **    Copyright © 1996-1997 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. /*
  23.  
  24.     Macintosh Developer Technical Support MP Sample Code
  25.  
  26.     SortPicts modified by Matthew Xavier Mora
  27.     to show MP sorting and blitting preemptively
  28.     
  29.     mxmora@best.com
  30.     last modified 11/2/98
  31.     
  32.     Added global Boolean for the case where MPLibrary is not around or doesn't load correctly
  33.     Added more blitters to support more bit depths. Blitters based on George Warnes CopyBlits.c
  34.     
  35. */
  36.  
  37. #define BUILDING_FOR_SYSTEM7_AND_SYSTEM8 1
  38.  
  39. #include "MPBlitter.h" // my simple 8 bit blitter
  40. #include "MPCopyBlits.h" // George's Blitters modified to us MP
  41.  
  42. #include "MPSortPicts.h"
  43. //#include "StandardMenus.h"
  44. #include <string.h>
  45. #include <strings.h>
  46. #include <Drag.h>
  47. #include <LowMem.h>
  48. #include <Sound.h>
  49. #include <Menus.h>
  50. /*
  51. enum {
  52.     kDragHasLeftSenderWindow    = (1 << 0),                        //drag has left the source window since TrackDrag
  53.     kDragInsideSenderApplication = (1 << 1),                    // drag is occurring within the sender application
  54.     kDragInsideSenderWindow        = (1 << 2)                        // drag is occurring within the sender window
  55. };
  56. */
  57. long        SortPicts::gNumWindowsCreated = 0;
  58. short        SortPicts::gPrefsRef = 0;
  59. short        gSortAlgorithm = 1;
  60. long        gNumSortPictResources = 0;
  61. MenuHandle    gPicturesMenu = 0;
  62. Boolean        gUseCoplandTasks = false;
  63. short        gTaskNumber = 1;
  64. extern         Boolean gHasMPLib;
  65. Rect gMenuRect;
  66.  
  67. pascal short MyMBarHook(Rect *menuRect);
  68. pascal short MyMBarHook(Rect *menuRect)
  69. {
  70.     (menuRect);    // #pragma unused(menuRect)
  71.  
  72.     // should check and see if menurect intersects any of our blitters
  73.     if (gHasMPLib) {
  74. //            MPYield();    // DON'T EVER CALL MPYIELD!
  75.     }
  76.     // RgnHandle tempRgn = NewRgn();
  77.     // RectRgn(tempRgn,menuRect);
  78.     // gMenuRect = *menuRect;
  79.     // CalcVisBehind(LMGetWindowList(),tempRgn);
  80.     
  81.     // YieldToAnyThread();
  82.     // DisposeRgn(tempRgn);
  83.     return 0;
  84. }
  85. pascal void MyMenuHook(void);
  86. pascal void MyMenuHook(void)
  87. {    
  88.  
  89.     if (gHasMPLib) {
  90.         MPYield();
  91.     }
  92.     
  93.     //RgnHandle tempRgn = NewRgn();
  94.     //RectRgn(tempRgn,&gMenuRect);
  95.     //CalcVisBehind(LMGetWindowList(),tempRgn);
  96.     
  97.     //YieldToAnyThread();
  98.     //DisposeRgn(tempRgn);
  99.  
  100. }
  101.  
  102. SortPicts::SortPicts()
  103. {
  104.     MenuHandle    sortAlgorithmMenu;
  105.     coplandTask = gUseCoplandTasks;
  106.     updateTime = TickCount();
  107.     lastHorizLeft = 0;
  108.     lastHorizRight = 0;
  109.     
  110.     randomSeed = TickCount() & 0x7FFFFFFF;
  111.  
  112.  
  113.     //LMGetMBarHook_UniversalProcPtr;
  114.     LMSetMBarHook(NewMBarHookProc(MyMBarHook));
  115.     LMSetMenuHook(NewMenuHookProc(MyMenuHook));
  116.     if( gPrefsRef == 0)
  117.     {
  118.         sortAlgorithmMenu = GetMenuHandle( mSortAlgorithm);
  119.         if( sortAlgorithmMenu)
  120.         {
  121.             CheckItem( sortAlgorithmMenu, gSortAlgorithm, true);
  122.             CheckItem( sortAlgorithmMenu, iUseCoplandTasks, gUseCoplandTasks);
  123.         }
  124.     
  125.         gPicturesMenu = GetMenuHandle( mPictureMenu);
  126.         if( gPicturesMenu)
  127.         {
  128.             AppendResMenu( gPicturesMenu, 'PICT');
  129.             InsertMenu( gPicturesMenu, -1);
  130.         
  131.         //    Count the number of Pictures in the menu
  132.             gNumSortPictResources = CountMItems( gPicturesMenu);
  133.         }
  134.  
  135.         gPrefsRef = OpenPreferencesResFile();
  136.         if( gPrefsRef > 0)
  137.         {
  138.             OpenDefaultSortWindows();
  139.         }
  140.     }
  141.     else
  142.     {
  143.         SelectRandomPicture();
  144.         CreateWindow( kNormalWindow);
  145.     }
  146. }
  147.  
  148. SortPicts::SortPicts( ConstStr255Param windowName)
  149. {
  150.     randomSeed = TickCount() & 0x7FFFFFFF;
  151.     coplandTask = gUseCoplandTasks;
  152.     updateTime = TickCount();
  153.     lastHorizLeft = 0;
  154.     lastHorizRight = 0;
  155.  
  156.     ::strcpy( (char *) sortPictsName, (char *) windowName);
  157.  
  158.     CreateWindow( kNormalWindow);
  159. }
  160.  
  161. SortPicts::~SortPicts()
  162. {
  163. /*    Terminate Threads    */
  164.  
  165.     if ( ! coplandTask ) {
  166.         DisposeThread( threadInfo, nil, true);
  167.     } else {
  168.         OSStatus    status = 0;
  169.         
  170.         // • kill the copland task.
  171.         //TerminateTask( myTaskID, kTaskOnly, 0, status );
  172.         MPTerminateTask( myTaskID,status );
  173.         UnuseSortData();
  174.     }
  175.     
  176. /*    Deallocate Memory    */
  177.     ReleaseSortData();
  178.     DisposeGWorld( sortGWorld);
  179. }
  180.  
  181.  
  182.  
  183.  
  184. WindowPtr SortPicts::MakeNewWindow(WindowPtr /* behindWindow */) 
  185. {
  186.     me = nil;
  187.     
  188.     if( PrepareGWorld( (ConstStr255Param) sortPictsName)) {
  189.  
  190.         if  (coplandTask) {
  191.             p2cstr(sortPictsName);
  192.             strcat((char*)sortPictsName,"(MP)");
  193.             c2pstr((char*)sortPictsName);
  194.         }
  195.         me = NewCWindow( nil, &windPictRect, (ConstStr255Param) sortPictsName,
  196.                     true, 0, (WindowPtr) -1, true, (long) this);
  197.         
  198.         
  199.         
  200.         CGrafPtr gp = (CGrafPtr)me;
  201.         
  202.         if ((gp  && coplandTask) && gp->portPixMap[0]->pixelSize != 8 ) {
  203.             
  204.             // no longer need this alert with Geowars help
  205.             //Alert(1128,nil);
  206.             
  207.         }
  208.         MakeThreaded();
  209.         
  210.     }
  211.         
  212.     return me;
  213. }
  214.  
  215.  
  216. void
  217. SortPicts::Activate(Boolean /* activating */)
  218. {
  219. //    DrawGrowIcon(fWindow);
  220. }
  221.  
  222.  
  223. void
  224. SortPicts::AdjustCursor(EventRecord * /* anEvent */)
  225. {
  226. }
  227.  
  228.  
  229. void
  230. SortPicts::Draw(void)
  231. {
  232.     
  233.  
  234.     
  235.     if ( coplandTask ) {
  236.         //CopyFromSortData();
  237.         CopyBlits    (    sortPixmap,
  238.                         ((CWindowPtr)me)->portPixMap,
  239.                          &sortRect,&sortRect,
  240.                         sortBlitterBusy);
  241.                         
  242.         /*MPCopyBits( ( BitMap *) *sortPixmap, 
  243.               ( BitMap *) (*(((CWindowPtr)me)->portPixMap)),
  244.               &sortRect,&sortRect,srcCopy,(RgnHandle)sortBlitterBusy); 
  245.         */
  246.         
  247.     } else {
  248.         sortPixmap = GetGWorldPixMap( sortGWorld);
  249.         LockPixels( sortPixmap );
  250.  
  251.         CopyBits( (const BitMap *) *sortPixmap, 
  252.               (const BitMap *) (*(((CWindowPtr)me)->portPixMap)),
  253.               &sortRect,&sortRect,srcCopy,nil);
  254.         UnlockPixels( sortPixmap );
  255.     }
  256. }
  257.  
  258.  
  259. // I modified the drag method to do the Live dragging
  260. void SortPicts::    Drag(Point startPoint)
  261. {
  262.  
  263.  
  264.     Rect r = {0,0,0,0};
  265.     Point origin = {0,0};    
  266.     Point diff;
  267.     EventRecord event;
  268.     GrafPtr tempPort;
  269.     KeyMap        theKeyMap;
  270.     Boolean     gLiveDrag;
  271. Boolean        commandKeyDown = false;
  272.  
  273.     GetKeys(theKeyMap);
  274.     commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  275.     gLiveDrag = (theKeyMap[1] >> 2) & 0x01 ;
  276.     
  277.     if (gLiveDrag || coplandTask) {    
  278.         GetPort(&tempPort);
  279.         SetPort(fWindow);    
  280.         (void)OSEventAvail(everyEvent,&event);
  281.         
  282.         LocalToGlobal(&origin);
  283.         diff.h = ( origin.h - event.where.h);
  284.         diff.v = ( origin.v -event.where.v);
  285.  
  286.         SetPort(tempPort);
  287.         while (StillDown()) {
  288.             EventRecord event;
  289.             (void)WaitNextEvent(everyEvent,&event,1,nil);
  290.             
  291.             if (event.what == updateEvt) {
  292.                 WindowRef window = (WindowRef)event.message;
  293.                 
  294.                 
  295.                 if (window) {
  296.                     TWindow * wobj;
  297.                     
  298.                     SetPort(window);
  299.                     BeginUpdate(window);
  300.                     wobj = GetWindowObject(window);
  301.  
  302.                     wobj->Draw();
  303.                     EndUpdate(window);
  304.                     SetPort(tempPort);
  305.                 }
  306.             }
  307.             if (gHasMPLib) {
  308.                 MPYield(); // for single CPU Macs
  309.             } else {
  310.                 YieldToAnyThread();
  311.             }
  312.             event.where.h += diff.h ;
  313.             event.where.v += diff.v;
  314.  
  315.             if (coplandTask) { // don't move the window if the blitter is blasting bits on the screen
  316.                 (void)MPEnterCriticalRegion(sortBlitterBusy, kDurationForever);
  317.             }
  318.             MoveWindow(fWindow,event.where.h,event.where.v,true);
  319.             if (coplandTask) {
  320.                 (void)MPExitCriticalRegion(sortBlitterBusy);
  321.             }
  322.             Idle(nil);
  323.             
  324.         }
  325.     }
  326.     
  327.     TWindow::Drag(startPoint); // call the real drag
  328.     if (!coplandTask) {
  329.         Idle(nil);
  330.     }
  331.     
  332. }
  333.  
  334.     
  335. void
  336. SortPicts::Idle(EventRecord * /* anEvent */)
  337. {
  338.  
  339.  
  340.  
  341.     if (  LMGetTicks() > updateTime )
  342.     {
  343.         UpdateSortPict();
  344.         updateTime = LMGetTicks() + kUpdateInterval; 
  345.         // when you're in macsbug LMTicks doesn't get updated
  346.         // so the sorting animation doesn't happen. But the 
  347.         // MPTask still sorts and blits the result to the screen
  348.         // even when in macsbug. Cool!
  349.         // I should probably use Uptime here
  350.  
  351.     } 
  352.  
  353.     
  354.  
  355.  
  356. }
  357.  
  358.  
  359. void
  360. SortPicts::KeyDown(EventRecord * /* anEvent */ )
  361. {
  362.     this->Select();
  363. }
  364.  
  365.  
  366. void
  367. SortPicts::AdjustForNewWindowSize(Rect * /* oldSize */, Rect * /* newSize */)
  368. {
  369. //    DrawGrowIcon(fWindow);    
  370. }
  371.  
  372.  
  373. Boolean
  374. SortPicts::Close(void)
  375. {
  376.         
  377.     DisposeThread( threadInfo, nil, true);
  378.     return TWindow::Close();
  379. }
  380.  
  381.  
  382. OSErr
  383. SortPicts::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
  384. {
  385.     RgnHandle    hiliteRgn = NewRgn();
  386.     
  387.     switch (dragMessage)
  388.     {
  389.         case kDragTrackingEnterWindow:
  390.             SetRectRgn( hiliteRgn,
  391.                         fWindow->portRect.left,
  392.                         fWindow->portRect.top,
  393.                         fWindow->portRect.right,
  394.                         fWindow->portRect.bottom);
  395.             ShowDragHilite(theDrag,hiliteRgn,true);
  396.             break;
  397.             
  398.         case    kDragTrackingLeaveWindow:
  399.             HideDragHilite(theDrag);
  400.             break;
  401.             
  402.         default:
  403.             break;
  404.     }
  405.     
  406.     DisposeRgn(hiliteRgn);
  407.     return(noErr);
  408. }
  409.  
  410.     
  411. OSErr
  412. SortPicts::HandleDrop(DragReference /* theDrag */)
  413. {
  414.     return(noErr);
  415. }
  416.